define(['angular', 'require', 'angular-route','angular-animate','ng-infinite-scroll'], function (angular, require) {
    var app = angular.module('webapp', [
        'ngRoute','ngAnimate','infinite-scroll'
    ]);

    app.config(['$routeProvider', '$controllerProvider',
        function($routeProvider, $controllerProvider) {
    		//
            var routeMap = {
//                '/': {                           //路由
//                    path: 'main-html.js',         //模块的代码路径
//                    controller: 'mainController'     //控制器名称
//                },
                '/shop_list':{//门店列表
                	path: 'www/shop/shopListCtrl.js',         //模块的代码路径
                    controller: 'shopListController'     //控制器名称
                },
                '/map_list':{//百度地图
                	path: 'www/shop/mapListCtrl.js',         //模块的代码路径
                    controller: 'mapListController'     //控制器名称
                },
                '/user':{//会员中心
                	path: 'www/user/userCtrl.js',         //模块的代码路径
                    controller: 'userController'     //控制器名称
                },
                '/collect':{//随拍收藏
                	path: 'www/trends/collectCtrl.js',         //模块的代码路径
                	controller: 'collectController'     //控制器名称
                },
                '/trends':{//
                	path: 'www/trends/trendsCtrl.js',         //模块的代码路径
                	controller: 'trendsController'     //控制器名称
                },
                '/picture_info/:newsId':{//随拍详情
                	path: 'www/trends/pictureInfoCtrl.js',         //模块的代码路径
                	controller: 'pictureInfoController'     //控制器名称
                },
                '/article_list/:catId':{//文章列表
                	path: 'www/article/articleListCtrl.js',         //模块的代码路径
                	controller: 'articleListController'     //控制器名称
                },
                '/article_info/:articleId':{//文章详情
                	path: 'www/article/articleInfoCtrl.js',         //模块的代码路径
                	controller: 'articleInfoController'     //控制器名称
                },
                '/publish':{//随拍
                	path: 'www/trends/publishCtrl.js',         //模块的代码路径
                	controller: 'publishController'     //控制器名称
                },
                '/community':{//首页
                	path: 'www/trends/communityCtrl.js',         //模块的代码路径
                	controller: 'communityController'     //控制器名称
                },
                '/community_new':{//首页
                	path: 'www/trends/communityNewCtrl.js',         //模块的代码路径
                	controller: 'communityNewController'     //控制器名称
                },
                '/trends_new':{//社区
                	path: 'www/trends/trendsNewCtrl.js',         //模块的代码路径
                	controller: 'trendsNewController'     //控制器名称
                },
            };
            var defaultRoute = '/community_new';              //默认跳转到某个路由

            $routeProvider.otherwise({redirectTo: defaultRoute});
            for (var key in routeMap) {
                $routeProvider.when(key, {
                	template:'',
                    controller: routeMap[key].controller,
                    resolve:{
                        keyName: requireModule(routeMap[key].path, routeMap[key].controller)
                    }
                });
            }

            function requireModule(path, controller) {
                return function ($route, $q,$rootScope) {
                	var deferred = $q.defer();//声明延后执行
//                    require([path], function (res) {
//                    	console.info(path);
//                        $controllerProvider.register(controller, res);      //由于是动态加载的controller，所以要先注册，再使用
//                        deferred.resolve();
//                    });
            		require([path], function (ret) {
                      $controllerProvider.register(controller, ret.controller);
                      $route.current.template = ret.tpl;
                      $rootScope.$apply(function() {
                    	  deferred.resolve(); //声明执行成功
                      });
                  });
                    return deferred.promise; //返回承诺，返回获取数据的API
//                	var deferred = $q.defer();
//                    require([path], function (ret) {
//                        $controllerProvider.register(controller, ret.controller);
//                        $route.current.template = ret.tpl;
//                        deferred.resolve();
//                    });
//                   return deferred.promise;
                }
            }

        }]);
    app.service('communService',function(){
		this.banners = [];
		this.articleCat = [];//分类
		this.articles = [];//文章详情
		this.news = [];
		this.page = 1;
		this.catId = "ALL";
		this.baseUrl = ip+"/";
		this.base = ip;
		this.adUrl = ip+"/"+"data/afficheimg/";
		this.busy = false;
		this.reSet = false;
		this.wxid = 0;
    	this.setCommun = function(commun){
    		this.banners = commun.banners;
    		this.articleCat = commun.articleCat;
    		this.articles = commun.articles;
    	};
    	this.setArticles = function(articles){
    		this.articles = articles;
    	};
    	this.initCatId = function(catId){
    		this.catId = catId;
    		this.articles = [];
    		this.page = 1;
    		
    	};
    });
    
    app.service('trendsService',function(){
		this.banners = [];
		this.newsCat = [];//分类
		this.newest = [];//最新帖子
		this.news_top = [];//精品
		this.catAllActive = "active";
		this.tabId = "newest";
		this.catId = "ALL";
		this.baseUrl = ip+"/";
		this.base = ip;
		this.adUrl = ip+"/"+"data/afficheimg/";
		this.busy = false;
		this.reSet = false;
		this.wxid = 0;
		this.setTrends = function(trends){
			this.banners = trends.banners;
			this.newsCat = trends.newsCat;//分类
			this.newest = trends.newest;//最新帖子
			this.news_top = trends.news_top;//精品
			this.wxid = trends.wxid;
		};
		this.setNewest = function(newest){
			this.newest = newest;//最新帖子
		};
		this.setNewsTop = function(news_top){
			this.news_top = news_top;
		};
		this.setTabId = function(tabId){
			this.tabId = tabId;
		};
		this.changeCatData = function(trends){
			this.newsCat = trends.newsCat;//分类
			this.newest = trends.newest;//最新帖子
			this.news_top =  trends.news_top;//精品
			this.catId = trends.catId;
			this.catAllActive = trends.catAllActive;
		}
    });
    
    
    app.directive('errSrc', function() {
    	  return {
    	    link: function(scope, element, attrs) {
    	      element.bind('error', function() {
    	        if (attrs.src != attrs.errSrc) {
    	          attrs.$set('src', attrs.errSrc);
    	        }
    	      });
    	    }
    	  }
    	});
    return app;
});
